package com.example.netty.common;

import com.example.netty.pojo.Offside;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class FieldClass {


    public static <T> T fieldVoluation(Class<T> clazz, String[] strings, Integer len) throws Exception {
        T instance = clazz.getDeclaredConstructor().newInstance();
        Field[] ListField = clazz.getDeclaredFields();
        Field[] fields = Arrays.stream(ListField).filter(field -> !field.isAnnotationPresent(Exclude.class)).toArray(Field[]::new);
        for (int i = 0; i < len; i++) {
            fields[i].setAccessible(true);
            if (fields[i].getType() == String.class) {
                if (fields[i].isAnnotationPresent(Reserve.class)) {
                    fields[i].set(instance, BigDecimal.valueOf(Long.valueOf(strings[i]),fields[i].getAnnotation(Reserve.class).value()).toString());
                } else {
                    fields[i].set(instance, BigDecimal.valueOf(Long.valueOf(strings[i])).toString());
                }
            }
        }
        // 方法体逻辑
        return instance;
    }

    public static <T> T fieldVoluation(Class<T> clazz) throws Exception {
        T instance = clazz.getDeclaredConstructor().newInstance();
        Field[] ListField = clazz.getDeclaredFields();
        Field[] fields = Arrays.stream(ListField).filter(field -> !field.isAnnotationPresent(Exclude.class)).toArray(Field[]::new);
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            if (fields[i].getType() == String.class) {
                if (fields[i].isAnnotationPresent(Reserve.class)) {
                    fields[i].set(instance, BigDecimal.valueOf(Long.valueOf("0"),fields[i].getAnnotation(Reserve.class).value()).toString());
                } else {
                    fields[i].set(instance, BigDecimal.valueOf(Long.valueOf("0")).toString());
                }
            }
        }
        // 方法体逻辑
        return instance;
    }

    public static <T, F> void fieldCompare(Class<T> clazz, T a, F b, List<Offside> OffsideList, String pid,boolean bool) throws Exception {


        Field[] ListField = clazz.getDeclaredFields();
        Field[] fields = Arrays.stream(ListField).filter(field -> field.isAnnotationPresent(Compare.class)).toArray(Field[]::new);
        String tableName=clazz.getSimpleName().replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();

        for (Field FieldA : fields) {
            ApiModelProperty annotation = FieldA.getAnnotation(ApiModelProperty.class);
//            log.info("属性名:{}",annotation.value());
            String value = FieldA.getAnnotation(Compare.class).value();
            String name = FieldA.getName();
            Field FieldB = b.getClass().getDeclaredField(name);
            FieldA.setAccessible(true);
            FieldB.setAccessible(true);
            BigDecimal va = BigDecimal.valueOf(Double.valueOf((String) FieldA.get(a))) ;
            BigDecimal vb = BigDecimal.valueOf(Double.valueOf((String) FieldB.get(b))) ;
            if(bool){
                switch (value) {
                    case ">":
                        if (va.compareTo(vb) > 0) {
                            Offside offside = query( annotation.value(),tableName,name,pid,va.toString(),vb.toString());
//                        Offside offside=query(jdbcTemplate,sql,tableName,name,pid,va.toString(),vb.toString());
                            OffsideList.add(offside);
                        }
                        break;
                    case "<":
                        if (va.compareTo(vb) < 0) {
                            Offside offside = query( annotation.value(),tableName,name,pid,va.toString(),vb.toString());
//                        Offside offside=query(jdbcTemplate,sql,tableName,name,pid,va.toString(),vb.toString());
                            OffsideList.add(offside);
                        }
                        break;
                    case "≥":
                        System.out.println(value);
                        break;
                    case "≤":
                        System.out.println(value);
                        break;
                }

            }


        }


    }

    public static <T> T fieldZero(T clazz) throws Exception {

        Field[] ListField = clazz.getClass().getDeclaredFields();
        Field[] fields = Arrays.stream(ListField).filter(field -> !field.isAnnotationPresent(Exclude.class)).toArray(Field[]::new);
        for (Field field:fields){
            field.setAccessible(true);
            if(field.get(clazz)==null){
                field.set(clazz,"0");
            }
        }
        return clazz;
    }

//    public static Offside query(JdbcTemplate jdbcTemplate,String sql,String tableName,String name,Integer pid,String va,String vb){
//        Offside offside=new Offside();
//        String cname = jdbcTemplate.queryForObject(sql, String.class, tableName, name);
//        String [] sp=cname.split("-");
//        offside.setPid(pid);
//        offside.setTablename(tableName);
//        offside.setColumnname(name);
//        offside.setKeyid(sp[0]);
//        offside.setKeyname(sp[1]);
//        offside.setKeyvalue(va);
//        offside.setWarnvalue(vb);
//        return  offside;
//    }
    public static Offside query( String fieldName,String tableName,String name,String pid,String va,String vb){
        Offside offside=new Offside();
        String [] sp=fieldName.split("-");
        offside.setPid(pid);
        offside.setTablename(tableName);
        offside.setColumnname(name);
        offside.setKeyid(sp[0]);
        offside.setKeyname(sp[1]);
        offside.setKeyvalue(va);
        offside.setWarnvalue(vb);
        return  offside;
    }

    public static void main(String[] args) throws Exception {

//        LeftMain leftMain = new LeftMain();
//        leftMain.setZjzs(BigDecimal.valueOf(13));
//        leftMain.setZwzzs(BigDecimal.valueOf(3));
//        LeftMainWarning leftMainWarning = new LeftMainWarning();
//        leftMainWarning.setZjzs(BigDecimal.valueOf(12));
//        leftMainWarning.setZwzzs(BigDecimal.valueOf(4));
//        List<Offside> OffsideList = null;
//        FieldClass.fieldCompare(LeftMain.class, leftMain, leftMainWarning, OffsideList, 11);

    }
}
